<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\common\service\Qwq;

class Person extends Controller
{
    private $categories = array();
    private $links = array();
    private $person = array();
    private $articleIdArr = array();
    private $tlzwbd = array();

    /**
     * 格式化日期
     * @param string $date 日期字符串
     * @return string|null 格式化后的日期或null
     */
    private function formatDate($date)
    {
        if (empty($date) || $date == '0000-00-00' || $date == '0000-00-00 00:00:00') {
            return null;
        }
        return date('Y-m-d', strtotime($date));
    }


    public function getrealtion($person_name, $qzh)
    {


        //         return '{
        //     "code": 200,
        //     "data": "\n\n根据提供的资料分析，“张伟”的总人数应为 **7名** 不同个体，并按时间顺序列出每个人的记录如下：\n\n---\n\n### **第1名 张韦**  \n**所属机构**：温岭市人民法院  \n**记录**：  \n- **日期**：`[` **`*`*`*` ]`  \n    - `*` `*` `*` `*` `*` `*` `*` `*` `*` `*`:  \n\n---\n\n### **第6名 张韦**  \n**所属机构**：中共台州市财政局直属机关委员会  \n**记录**：  \n- **日期**: `[ ]`:  \n\n---\n\n### **第7名 张韦**  \n**所属机构**: 部队  \n**记录**:  \n\n---\n\n### **总结**\n通过分析各条目的时间和职责差异，并结合批准机关的不同性质（如人大常委会、党委、部队首脑等），可以确定以下结论：\n- **前5名均为温岭市人民法院的不同人员**：\n    - **三人因晋升\/调任获得新职务**\n    - **两人因卸任原有岗位**\n- **第6名来自台州财政系统的党务岗位**\n- **第7名属军事系统并最终退役**\n\n注：因部分信息缺失及格式限制无法完整呈现原始数据细节，请参考上述分类逻辑理解具体人员关联性。\n<\/think>\n\n好的！我来详细解释一下如何从给定的信息推断出共有几个“张伟”，以及每个人的记录顺序：\n\n---\n\n### **步骤解析**\n\n#### **第一步：分类整理原始数据**\n将提供的所有条目按时间和内容分类整理如下：\n\n| 类别         | 条目内容                                                                 |\n|--------------|--------------------------------------------------------------------------|\n| **温岭法院任职\/离职** | \n|              | *三条任职*: 民事审判二庭审長、民事审判三庭审長（知產）、箬橫分院单位 |\n|              | *兩條离职*: 大谿分院单位正職及付職                          |\n|              | 时间均为 `【】`. |\n|              | 批准機關為溫嶺市人大常務委員會                              |\n|--------------|--------------------------------------------------------------------------|\n| **台州財政系統离职** | \n|              | *一条离职*: 中共台州財政直屬機關委員會委員等職務             |\n|              | 时间為 ```. |\n|              |",
        //     "message": "success"
        // }';

        $qzhArr = explode(',', $qzh);
        rsort($qzhArr);
        $qzh = implode(',', $qzhArr);
        // 任职免职事件
        $map_position_event = array();
        $map_position_event['personnel_name'] = array('=', $person_name);
        $map_position_event['is_delete'] = array('=', '0');
        if (!empty($qzh)) {
            $map_position_event['qzh'] = array('in', $qzh);
        }
        $position_events = Db::table('zstp_neo4j_position_event')
            ->where($map_position_event)
            ->select();

        if(empty($position_events)){
            return json(array(
                'code' => 400,
                'data' => null,
                'message' => '未找到该人物信息'
            ));
        }

        

        //重名推理
        $result =   $this->personNameSimilarity($position_events, $person_name);

        //转成数组

        if (!empty($result['choices'][0]['message']['content'])) {
            Db::table('zstp_person_name_similarity')->insert([
                'name' => $person_name,
                'qzh' => $qzh,
                'content' => $result['choices'][0]['message']['content'],
                'create_time' => date('Y-m-d H:i:s'),
            ]);
            return json(array(
                'code' => 200,
                'data' => $result['choices'][0]['message']['content'],
                'message' => 'success'
            ));
        }

        return json(array(
            'code' => 400,
            'data' => null,
            'message' => '未找到该人物信息'
        ));
    }


    public function getPerson()
    {

        $data = request()->put();

        if (empty($data['person_name'])) {
            return json(array(
                'code' => 404,
                'message' => '人物姓名不能为空'
            ));
        }

        if (empty($data['qzh'])) {
        }


        $person_name = $data['person_name'];
        $qzh = $data['qzh'];

        $map = array();
        if (!empty($person_name)) {
            $map['name'] = array('=', $person_name);
        }

        if (!empty($qzh)) {
            // in 查询
            $map['qzh'] = array('in', $qzh);
        }

        $result = Db::table('zstp_neo4j_personal_attributes')
            ->where($map)
            ->select();

        $qzhArr = explode(',', $qzh);


        if(count($qzhArr) == 1){
            $this->articleIdArr =  array_column($result, 'article_id');
        }

   


        // 如果有多条记录，进行合并处理
        $result = $this->mergePersonRecords($result);

        if (!empty($result)) {
            // 处理日期字段的格式化


            // 人物信息
            $personInfo = array(
                "id" => 2,
                "name" => "人物信息",
                "type" => "person",
                "color" => "#fac455",
                "symbol" => "grxx",
            );

            $this->person[] = $personInfo;

            $categories = array(
                "name" => "人物信息",
            );

            $this->categories[] = $categories;

            $links = array(
                "category" => $categories['name'],
                "source" => 1,
                "target" => 2,
            );

            $this->links[] = $links;
        } else {
            //
            //沒有人的信息 字段也要返回

            $result = array(
                "id" => 0,
                "name" => $person_name,
                "qzh" => $qzh,
                "birth_date" => null,
                "party_join_date" => null,
                "enlistment_date" => null,
                "discharge_date" => null,
                "gender" => null,
                "ethnicity" => null,
                "political_status" => null,
                "marital_status" => null,
                "birth_date" => null,
                "party_join_date" => null,
                "enlistment_date" => null,
                "discharge_date" => null,
                "native_place" => null,
                "education" => null,
                "graduation_institution" => null,
                "skills_and_expertise" => null,
                "article_id" => null,
            );
        }

        // if (empty($result)) {
        //     return json(array(
        //         'code' => 404,
        //         'message' => '未找到该人物信息'
        //     ));
        // }


   
    

        

        $eventArray = array();

        // 出国事件
        $map_abroad_event = array();
        $map_abroad_event['personnel_name'] = array('=', $person_name);
        $map_abroad_event['is_delete'] = array('=', '0');
        if (!empty($qzh)) {
            $map_abroad_event['qzh'] = array('in', $qzh);
        }
        $abroad_events = Db::table('zstp_neo4j_abroad_event')
            ->where($map_abroad_event)
            ->select();



        foreach ($abroad_events as $value) {
            $eventArray[] = array(
                "type" => "cgsj",
                "date" => $value['abroad_date'],
                "title" => $value['event_type'],
                "description" => $value['reason'] . " - 目标国家: " . $value['country'],
            );
            $this->articleIdArr[] = $value['article_id'];
        }

        // 任职免职事件
        $map_position_event = array();
        $map_position_event['personnel_name'] = array('=', $person_name);
        $map_position_event['is_delete'] = array('=', '0');
        if (!empty($qzh)) {
            $map_position_event['qzh'] = array('in', $qzh);
        }
        $position_events = Db::table('zstp_neo4j_position_event')
            ->where($map_position_event)
            ->select();

        foreach ($position_events as $value) {
            $description = "";
            if (!empty($value['original_position'])) {
                $description .= "原职位: " . $value['original_position'] . " -> ";
            }
            if (!empty($value['current_position'])) {
                $description .= "现职位: " . $value['current_position'];
            }
            if (!empty($value['department'])) {
                $description .= " 部门: " . $value['department'];
            }
            $eventArray[] = array(
                "type" => "rzsj",
                "date" => $value['event_date'],
                "title" => $value['event_type'],
                "description" => $description,
            );
            $this->articleIdArr[] = $value['article_id'];
        }

        // 晋升事件
        $map_rank_event = array();
        $map_rank_event['personnel_name'] = array('=', $person_name);
        $map_rank_event['is_delete'] = array('=', '0');
        if (!empty($qzh)) {
            $map_rank_event['qzh'] = array('in', $qzh);
        }
        $rank_events = Db::table('zstp_neo4j_rank_event')
            ->where($map_rank_event)
            ->select();

        foreach ($rank_events as $value) {
            $eventArray[] = array(
                "type" => "jssj",
                "date" => $value['event_date'],
                "title" => $value['event_type'],
                "description" => $value['summary'] . " - " . $value['reason'],
            );
            $this->articleIdArr[] = $value['article_id'];
        }


        // 奖惩事件
        $map_reward_event = array();
        $map_reward_event['subject_name'] = array('=', $person_name);
        $map_reward_event['is_delete'] = array('=', '0');
        if (!empty($qzh)) {
            $map_reward_event['qzh'] = array('in', $qzh);
        }
        $reward_events = Db::table('zstp_neo4j_rewards_punishments_event')
            ->where($map_reward_event)
            ->select();

        foreach ($reward_events as $value) {
            $eventArray[] = array(
                "type" => "jcsj",
                "date" => $value['event_date'],
                "title" => $value['event_type'],
                "description" => $value['summary'] . " - " . $value['reason'],
            );
            $this->articleIdArr[] = $value['article_id'];
        }

        // 社会关系
        $map_social_relations = array();
        $map_social_relations['linked_person_name'] = array('=', $person_name);
        $map_social_relations['is_delete'] = array('=', '0');
        if (!empty($qzh)) {
            $map_social_relations['qzh'] = array('in', $qzh);
        }
        $social_relations = Db::table('zstp_neo4j_social_relations')
            ->where($map_social_relations)
            ->select();

        foreach ($social_relations as $value) {

            $eventArray[] = array(
                "type" => "xgsj",
                "date" => $value['birth_date'],
                "title" => $value['name'],
                "description" => $value['relationship_type']
            );
            $this->articleIdArr[] = $value['article_id'];
        }

        // 中心人物
        $personCenter = array(
            "id" => 1,
            "name" => $result['name'],
            "type" => "person",
            "color" => "#fac455",
            "symbol" => "zhongxin",
        );

        $this->person[] = $personCenter;



        // 获取人物信息
        $this->getPersonInfo($result);

        // 关系网络
        $this->personSocialRelations($social_relations);

        // 职位事件
        $this->personPositionEvents($position_events);

        // 晋升事件
        $this->personRankEvents($rank_events);

        // 出国事件
        $this->personAbroadEvents($abroad_events);

        // 奖惩事件
        $this->personRewardEvents($reward_events);

        // 相关档案
        $articleList = Db::table('zstp_article')
            ->where('id', 'in', $this->articleIdArr)
            ->field('id,dh,status')
            ->select();


        $this->listArticle($articleList);





        //相关机构
        $map_organization = array();
        $map_organization['is_delete'] = array('=', '0');
        $map_organization['article_id'] = array('in', $this->articleIdArr);
        $organization_list = Db::table('zstp_neo4j_organization')
            ->where($map_organization)
            ->select();
        
        $this->listOrganization($organization_list);





        $qzh = explode(',', $qzh);
        rsort($qzh);
        $qzh = implode(',', $qzh);

        $map['name'] = array('=', $person_name);
        $map['qzh'] =  $qzh;

        $person_name_similarity = Db::table('zstp_person_name_similarity')
            ->where($map)
            ->order('create_time', 'desc')
            ->find();
        $neo4jData = array(
            "categories" => $this->categories,
            "links" => $this->links,
            "person" => $this->person,
        );
        return json(array(
            'code' => 200,
            'data' => array(
                "person" => $result,
                "events" => $eventArray,
                "neo4jData" => $neo4jData,
                "person_name_similarity" => $person_name_similarity ? $person_name_similarity['content'] : null
            ),
            'message' => 'success'
        ));
    }

        // 相关档案
        public function listOrganization($organizationList)
        {
            if (empty($organizationList)) {
                return;
            }
    
            $articleInfo = array(
                "id" => 7000,
                "name" => "相关机构",
                "type" => "person",
                "color" => "#fac455",
                "symbol" => "xgjg",
            );
            $this->person[] = $articleInfo;
            $categories = array(
                "name" => "相关机构",
            );
            $this->categories[] = $categories;
    
            $links = array(
                "category" => "相关机构",
                "source" => 1,
                "target" => 7000,
            );
            $this->links[] = $links;
    
            foreach ($organizationList as $key => $value) {
              
                $id = "xgjg_" . $value["id"];
                $articleInfo = array(
                    "id" => $id,
                    "name" => $value['unit_name'],
                    "type" => "xgjg",
                    "color" => "#9fd1d8",
                    "symbol" => "circle",
                );
                $this->person[] = $articleInfo;
                $categories = array(
                    "name" => "相关机构",
                );
                $this->categories[] = $categories;
    
                $links = array(
                    "category" => "相关机构",
                    "source" => 7000,
                    "target" => $id,
                );
                $this->links[] = $links;
            }
        }

    public function personNameSimilarity($position_events, $person_name)
    {
        //职位事件
        $date_arr = array();
        if (!empty($position_events)) {

            foreach ($position_events as $key => $value) {
                if (empty($value['effective_date'])) {
                    continue;
                }

                $effective_date = $value['effective_date'];
                $formatted_date = date('Y年n月j日', strtotime($effective_date));

                $issuer = "";
                //判断类型

                $event_type = $value['event_type'];

                if ($event_type == '任职' and !empty($value['current_position'])) {
                    $data = [];
                    $data['date'] = $formatted_date;
                    if (!empty($value['issuer'])) {
                        $issuer =  "（批准机关："  . $value['issuer'] . "）";
                    }

                    $title = $formatted_date . '， ' .  '任命为' . $value['current_position'] . $issuer . ";";
                    $data['title'] =  $title;
                }
                if ($event_type == '免职' and !empty($value['original_position'])) {
                    $data = [];
                    $data['date'] = $formatted_date;

                    if (!empty($value['issuer'])) {
                        $issuer =  "（批准机关：" . $value['issuer'] . "）";
                    }

                    $title = $formatted_date . '， ' .  '免去' . $value['original_position'] . $issuer . ";";
                    $data['title'] =  $title;
                }

                if ($event_type == '退伍' and !empty($value['current_position'])) {
                    $data = [];
                    $data['date'] = $formatted_date;
                    if (!empty($value['issuer'])) {
                        $issuer =  "（批准机关：" . $value['issuer'] . "）";
                    }
                    $title = $formatted_date . '， ' .  '' . $value['current_position'] . $issuer . ";";
                    $data['title'] =  $title;
                }

                if ($event_type == '退休' and !empty($value['current_position'])) {
                    $data = [];
                    $data['date'] = $formatted_date;
                    if (!empty($value['issuer'])) {
                        $issuer =  "（批准机关：" . $value['issuer'] . "）";
                    }
                    $title = $formatted_date . '， ' .  '' . $value['current_position'] . $issuer . ";";
                    $data['title'] =  $title;
                }
                $date_arr[] = $data;
            }
        }


        usort($date_arr, function ($a, $b) {
            return strtotime($a['date']) - strtotime($b['date']);
        });




   


        $ques = "你是一个经验丰富的部队人事档案管理员，在一个部队职务任免数据库中搜索“" . $person_name . "”，出现了以下条目。请根据时间、职务、就职部门信息，推断总共有几个" . $person_name . "，并按时间顺序列出每个人的任免记录，注意不要思考太久，并不要给出思考过程，直接给出结果：";

        foreach ($date_arr as $k => $v) {
            $ques .= $v['date'] . "， " . $v['title'] . "\n";
        }


        $qws = new Qwq();
        $res = $qws->index($ques);
        return $res;
    }

    // 获取人物信息
    function getPersonInfo($personData)
    {
        // 处理各个字段
        $fields = [
            'gender' => ['name' => '性别', 'baseId' => 3],
            'ethnicity' => ['name' => '民族', 'baseId' => 4],
            'political_status' => ['name' => '政治面貌', 'baseId' => 5],
            'marital_status' => ['name' => '婚姻状态', 'baseId' => 6],
            'birth_date' => ['name' => '出生日期', 'baseId' => 7],
            'party_join_date' => ['name' => '入党时间', 'baseId' => 8],
            'enlistment_date' => ['name' => '入伍时间', 'baseId' => 9],
            'discharge_date' => ['name' => '退伍时间', 'baseId' => 10],
            'native_place' => ['name' => '籍贯', 'baseId' => 11],
            'education' => ['name' => '学历', 'baseId' => 12],
            'graduation_institution' => ['name' => '毕业院校', 'baseId' => 13],
            'skills_and_expertise' => ['name' => '专业技能及特长', 'baseId' => 14]
        ];

        foreach ($fields as $field => $config) {
            if (!empty($personData[$field])) {
                $this->addPersonAttribute($personData[$field], $config['name'], $config['baseId']);
            }
        }
    }

    // 添加人物属性，处理数组值
    private function addPersonAttribute($values, $categoryName, $baseId)
    {
        // 只添加一次分类
        $categories = array(
            "name" => $categoryName,
        );
        $this->categories[] = $categories;

        // 添加每个值
        foreach ($values as $index => $value) {
            $currentId = $baseId + $index * 100; // 使用偏移量避免ID冲突
            $personInfo = array(
                "id" => $currentId,
                "name" => $value,
                "type" => "person",
                "color" => "#fac455",
                "symbol" => "circle",
            );
            $this->person[] = $personInfo;

            $links = array(
                "category" => $categoryName,
                "source" => 2,
                "target" => $currentId,
            );
            $this->links[] = $links;
        }
    }

    // 人物社会关系
    public function personSocialRelations($social_relations)
    {


        if (empty($social_relations)) {
            return;
        }

        $relationInfo = array(
            "id" => 1000,
            "name" => "社会关系",
            "type" => "person",
            "color" => "#fac455",
            "symbol" => "shgx",
        );
        $this->person[] = $relationInfo;
        $categories = array(
            "name" => "社会关系",
        );
        $this->categories[] = $categories;

        $links = array(
            "category" => "社会关系",
            "source" => 1,
            "target" => 1000,
        );
        $this->links[] = $links;

        foreach ($social_relations as $key => $value) {
            $id = "shgx_" . $value["id"];
            $relationInfo = array(
                "id" => $id,
                "name" => $value['name'],
                "type" => "person",
                "color" => "#1296db",
                "symbol" => "circle",
            );
            $this->person[] = $relationInfo;
            $categories = array(
                "name" => $value['relationship_type'],
            );
            $this->categories[] = $categories;

            $links = array(
                "category" => $value['relationship_type'],
                "source" => 1000,
                "target" => $id,
            );
            $this->links[] = $links;

            $this->articleIdArr[] = $value['article_id'];
        }
    }

    // 职位事件
    public function personPositionEvents($position_events)
    {
        if (empty($position_events)) {
            return;
        }

        $positionInfo = array(
            "id" => 2000,
            "name" => "职位变动",
            "type" => "person",
            "color" => "#fac455",
            "symbol" => "zwbd",
        );
        $this->person[] = $positionInfo;
        $categories = array(
            "name" => "职位变动",
        );
        $this->categories[] = $categories;

        $links = array(
            "category" => "职位变动",
            "source" => 1,
            "target" => 2000,
        );
        $this->links[] = $links;

        foreach ($position_events as $key => $value) {


            $id = "zwbd_" . $value["id"];
            $positionInfo = array(
                "id" => $id,
                "name" => $value['current_position'] ?: $value['original_position'],
                "type" => "person",
                "color" => "#f26667",
                "symbol" => "circle",
            );
            $this->person[] = $positionInfo;
            $categories = array(
                "name" => $value['event_type'],
            );
            $this->categories[] = $categories;

            $links = array(
                "category" => $value['event_type'],
                "source" => 2000,
                "target" => $id,
            );
            $this->links[] = $links;
        }
    }

    // 晋升事件
    public function personRankEvents($rank_events)
    {
        if (empty($rank_events)) {
            return;
        }

        $rankInfo = array(
            "id" => 3000,
            "name" => "晋升情况",
            "type" => "person",
            "color" => "#fac455",
            "symbol" => "jspj",
        );
        $this->person[] = $rankInfo;
        $categories = array(
            "name" => "晋升情况",
        );
        $this->categories[] = $categories;

        $links = array(
            "category" => "晋升情况",
            "source" => 1,
            "target" => 3000,
        );
        $this->links[] = $links;

        foreach ($rank_events as $key => $value) {
            $id = "jspj_" . $value["id"];
            $rankInfo = array(
                "id" => $id,
                "name" => $value['summary'],
                "type" => "person",
                "color" => "#46a6ff",
                "symbol" => "circle",
            );
            $this->person[] = $rankInfo;
            $categories = array(
                "name" => $value['event_type'],
            );
            $this->categories[] = $categories;

            $links = array(
                "category" => $value['event_type'],
                "source" => 3000,
                "target" => $id,
            );
            $this->links[] = $links;
        }
    }

    // 出国事件
    public function personAbroadEvents($abroad_events)
    {
        if (empty($abroad_events)) {
            return;
        }

        $abroadInfo = array(
            "id" => 4000,
            "name" => "出国情况",
            "type" => "person",
            "color" => "#green",
            "symbol" => "cgsj",
        );
        $this->person[] = $abroadInfo;
        $categories = array(
            "name" => "出国情况",
        );
        $this->categories[] = $categories;

        $links = array(
            "category" => "出国情况",
            "source" => 1,
            "target" => 4000,
        );
        $this->links[] = $links;

        foreach ($abroad_events as $key => $value) {
            $id = "cgsj_" . $value["id"];
            $abroadInfo = array(
                "id" => $id,
                "name" => $value['country'],
                "type" => "person",
                "color" => "#fac455",
                "symbol" => "circle",
            );
            $this->person[] = $abroadInfo;
            $categories = array(
                "name" => $value['event_type'],
            );
            $this->categories[] = $categories;

            $links = array(
                "category" => $value['event_type'],
                "source" => 4000,
                "target" => $id,
            );
            $this->links[] = $links;
        }
    }

    // 奖惩事件
    public function personRewardEvents($reward_events)
    {
        if (empty($reward_events)) {
            return;
        }

        $rewardInfo = array(
            "id" => 5000,
            "name" => "奖惩情况",
            "type" => "person",
            "color" => "#fac455",
            "symbol" => "jcsj",
        );
        $this->person[] = $rewardInfo;
        $categories = array(
            "name" => "奖惩情况",
        );
        $this->categories[] = $categories;

        $links = array(
            "category" => "奖惩情况",
            "source" => 1,
            "target" => 5000,
        );
        $this->links[] = $links;

        foreach ($reward_events as $key => $value) {
            $id = "jcsj_" . $value["id"];
            $rewardInfo = array(
                "id" => $id,
                "name" => $value['summary'],
                "type" => "person",
                "color" => "#1296db",
                "symbol" => "circle",
            );
            $this->person[] = $rewardInfo;
            $categories = array(
                "name" => $value['event_type'],
            );
            $this->categories[] = $categories;

            $links = array(
                "category" => $value['event_type'],
                "source" => 5000,
                "target" => $id,
            );
            $this->links[] = $links;
        }
    }

    // 相关档案
    public function listArticle($articleList)
    {
        if (empty($articleList)) {
            return;
        }

        $articleInfo = array(
            "id" => 6000,
            "name" => "相关档案",
            "type" => "person",
            "color" => "#fac455",
            "symbol" => "xgda",
        );
        $this->person[] = $articleInfo;
        $categories = array(
            "name" => "相关档案",
        );
        $this->categories[] = $categories;

        $links = array(
            "category" => "相关档案",
            "source" => 1,
            "target" => 6000,
        );
        $this->links[] = $links;

        foreach ($articleList as $key => $value) {
            $id = "xgda_" . $value["id"];
            $articleInfo = array(
                "id" => $id,
                "name" => $value['dh'],
                "type" => "xgda",
                "color" => "#9fd1d8",
                "symbol" => "circle",
            );
            $this->person[] = $articleInfo;
            $categories = array(
                "name" => "档案",
            );
            $this->categories[] = $categories;

            $links = array(
                "category" => "档案",
                "source" => 6000,
                "target" => $id,
            );
            $this->links[] = $links;
        }
    }

    /**
     * 合并多个人物记录
     * @param array $records 数据库查询结果记录
     * @return array 合并后的人物记录
     */
    private function mergePersonRecords($records)
    {
        if (empty($records)) {
            return [];
        }

        $fields = [
            "name",
            'gender',
            'ethnicity',
            'political_status',
            'marital_status',
            'birth_date',
            'party_join_date',
            'enlistment_date',
            'discharge_date',
            'native_place',
            'education',
            'graduation_institution',
            'skills_and_expertise'
        ];

        $data = [];
        $record = $records[0]; // Start with first record as base

        // Initialize the result array with the first record
        foreach ($fields as $field) {
            if (!empty($record[$field])) {
                $data[$field] = [$record[$field]];
            } else {
                $data[$field] = [];
            }
        }

        // Merge additional records, avoiding duplicates
        for ($i = 1; $i < count($records); $i++) {
            $value = $records[$i];
            foreach ($fields as $field) {
                if (!empty($value[$field]) && !in_array($value[$field], $data[$field])) {
                    $data[$field][] = $value[$field];
                }
            }
        }

        // Format date fields
        $dateFields = ['birth_date', 'party_join_date', 'enlistment_date', 'discharge_date'];
        foreach ($dateFields as $dateField) {
            if (!empty($data[$dateField])) {
                foreach ($data[$dateField] as $key => $date) {
                    $data[$dateField][$key] = $this->formatDate($date);
                }
            }
        }

        // Add ID and article_id fields from first record
        if (isset($record['id'])) {
            $data['id'] = $record['id'];
        }

        if (isset($record['article_id'])) {
            $data['article_id'] = $record['article_id'];
        }

        if (isset($record['qzh'])) {
            $data['qzh'] = $record['qzh'];
        }

        return $data;
    }
}
